<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mycommon {	
	
	function getSaveSqlStr($str)
	{
		if(get_magic_quotes_gpc())
		{
			return $str;
		}
		else
		{
			//return mysql_real_escape_string($str);
			$CI = &get_instance();
			return mysqli_real_escape_string($CI->db->conn_id, $str);
		}
	}
	function validEmail($email)
    {
		return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $email)) ? false : true;
	}
	
	function validUsername($str) 
	{
	    return preg_match('/^([a-zA-Z])([a-z0-9_])*$/',$str) ? true : false;
	}
    function validMd5($str)
    {
    	$str = trim($str);
    	if($str == '')
    	{
    		return false;
    	}
    	$chk = (preg_match('/^[a-f0-9]{32}$/i', $str)) ? true : false;
    	return $chk;
    }
    /*---------GET CURRENT_URL------------*/
    function selfURL()
    {
        $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
        $protocol = $this->strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
        $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
        return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
    }
    function strleft($s1, $s2)
    {
        return substr($s1, 0, strpos($s1, $s2));
    }
    /*----GET STORE FROM str of MODEL------------*/
    function getNameStore($strCallStore)
    {
        $arrStr = explode("(", $strCallStore);
        return $arrSt = substr(trim($arrStr[0]), 4);
    }
    
    function sanitize($text) {
    	$text = htmlspecialchars($text, ENT_QUOTES);
    	$text = str_replace("\n\r","\n",$text);
    	$text = str_replace("\r\n","\n",$text);
    	$text = str_replace("\n","<br>",$text);
    	return $text;
    }
    function createRangeRule($val, $limit)
    {
        //map 13 field
        $arr = array();
        if($val > 0){
            for($j=$val-6;$j<=$val+6;$j++)
            {
                if($j>$limit)
                {
                    $l = - $limit + ($j - $limit) - 1;
                    $arr[] = $l;
                }
                else
                {
                    $arr[] = $j;
                }
            }
        }
        else
        {
            $arr_m_1 = array();
            $arr_m_2 = array();
            if(abs($val)+6>$limit)//$val < 0
            {
                $pos = $limit - (12 - ($limit+$val+6));
                for($j=-$limit;$j<=$val+6;$j++)
                {
                    $arr_m_2[] = $j;
                }
                for($u=$pos+1;$u<=$limit;$u++)
                {
                    $arr_m_1[] = $u;
                }
                $arr = array_merge($arr_m_1, $arr_m_2);
            }
            else
            {
                for($j=$val-6;$j<=$val+6;$j++)
                {
                    $arr[] = $j;
                }
            }
        }
        return $arr;
    }
    function distanceTime($from, $to, $speed, $WORD_MAX)
    {
        $xdistance = $to['x'] > $from['x'] ? $to['x'] - $from['x'] : $from['x'] - $to['x'];
        $xdistance += ($to['x']<0&&$from['x']>0) || ($to['x']>0&&$from['x']<0) ? 1:0;
		if($xdistance >= $WORD_MAX) {
			while($xdistance >= $WORD_MAX):
			$xdistance -= $WORD_MAX;
			endwhile;
		}
        $ydistance = $to['y'] > $from['y'] ? $to['y'] - $from['y'] : $from['y'] - $to['y'];
        $ydistance += ($to['y']<0&&$from['y']>0) || ($to['y']>0&&$from['y']<0) ? 1:0;
		if($ydistance >= $WORD_MAX) {
			while($ydistance >= $WORD_MAX):
			$ydistance -= $WORD_MAX;
			endwhile;
		}
        $distance = $xdistance+$ydistance;
        return round(($distance/$speed) * 3600 );
    }
    /*----format military------------*/
    function formatMilitary($number, $type)
	{
		if($type == 1)
		{
			/*
			* luc quan
			*/
			// tieu doi 
			if($number < 36) // neu so luong nho hon trung doi
			{
				/*
				* tieu doi 12 nguoi
				*/
				$nSquad = (int)($number/12);
				if($nSquad <= 2)
				{
					$name = $squad = $nSquad.' tiểu đội';
				}
			}
			else if($number < 100)
			{
				/*
				* trung doi 36 nguoi
				*/
				$nPlatoon = (int)($number/36);
				if($nPlatoon <= 2)
				{
					$name = $platoon = $nPlatoon.' trung đội';
				}
			}
			else if($number < 324)
			{
				/*
				* dai doi 100 nguoi
				*/
				$nTroop = (int)($number/100);
				if($nTroop <= 2)
				{
					$name = $troop = $nTroop.' đại đội';
				}
			}
			else if($number < 3000)
			{
				/*
				* tieu doan 12 nguoi
				* 3 dai doi bo binh
				* 1 trung doi hoa luc - 3 khau phao va 24 nguoi
				*/
				$nSquadron = (int)($number/324);
				if($nSquadron <= 2)
				{
					$name = $squadron = $nSquadron.' tiểu đoàn';
				}
			}
			else if($number < 3500)
			{
				/*
				* tieu doan 12 nguoi
				* 3 dai doi bo binh
				* 1 trung doi hoa luc
				*/
				$nRegiment = (int)($number/3000);
				if($nRegiment == 1)
				{
					$name = $regiment = $nRegiment.' trung đoàn';
				}
			}
			else if($number < 9000)
			{
				/*
				* tieu doan 12 nguoi
				* 3 dai doi bo binh
				* 1 trung doi hoa luc
				*/
				$nBrigade = (int)($number/3000);
				if($nBrigade <= 2)
				{
					$name = $brigade = $nBrigade.' lữ đoàn';
				}
			}
			else if($number < 50000)
			{
				/*
				* tieu doan 12 nguoi
				* 3 dai doi bo binh
				* 1 trung doi hoa luc
				*/
				$nDivision = (int)($number/9000);
				if($nDivision <= 4)
				{
					$name = $division = $nDivision.' sư đoàn';
				}
			}
			else if($number < 50000)
			{
				/*
				* tieu doan 12 nguoi
				* 3 dai doi bo binh
				* 1 trung doi hoa luc
				*/
				$nCorps = (int)($number/9000);
				$name = $corps = $nCorps.' quân đoàn';
			}
		}
		return $name;
	}
}